package com.party.core.service.wallet.biz;

import com.party.common.utils.StringUtils;
import com.party.common.utils.VerifyCodeUtils;
import com.party.core.model.BaseModel;
import com.party.core.model.crowdfund.MemberWithdrawalFund;
import com.party.core.model.member.MemberBank;
import com.party.core.model.wallet.WithdrawalAuditOperate;
import com.party.core.model.wallet.WithdrawalStatus;
import com.party.core.model.wallet.Withdrawals;
import com.party.core.service.crowdfund.IMemberWithdrawalFundService;
import com.party.core.service.wallet.IWithdrawalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Author: Administrator.
 * @Description: 提现
 * @Date:Created in 2017/10/25 0025 下午 16:50.
 */
@Service
public class WithdrawalsBizService {
    @Autowired
    private IWithdrawalService withdrawalService;

    @Autowired
    private IMemberWithdrawalFundService memberWithdrawalFundService;

    /**
     * 提现总额
     *
     * @param memberId 用户id
     * @param type     提现类型 1：余额 2：佣金
     * @return
     */
    public Double getWithdrawalTotal(String memberId, Integer type,String businessId) {
        Withdrawals withdrawal = new Withdrawals();
        withdrawal.setCreateBy(memberId);
        withdrawal.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
        withdrawal.setType(type);
        withdrawal.setBusinessId(businessId);
        Map<String, Object> params = new HashMap<String, Object>();
        Set<Integer> status = new HashSet<Integer>();
        status.add(WithdrawalStatus.STATUS_HAVE_PAID.getCode()); // 已付款
        status.add(WithdrawalStatus.STATUS_IN_REVIEW.getCode()); // 处理中
        status.add(WithdrawalStatus.STATUS_AUDIT_PASS.getCode()); // 已通过
        params.put("status", status);
        Double withdrawalTotal = withdrawalService.getTotalPayment(withdrawal, params);
        return withdrawalTotal == null ? 0d : withdrawalTotal;
    }

    /**
     * 格式化银行账号
     *
     * @param account
     * @return
     */
    public String formatAccountNumber(String account) {
        if (StringUtils.isEmpty(account)) {
            return account;
        }
        StringBuilder sb = new StringBuilder(account);
        if (StringUtils.isNotEmpty(account)) {
            int length = account.length() / 4 + account.length();

            for (int i = 0; i < length; i++) {
                if (i % 5 == 0) {
                    sb.insert(i, " ");
                }
            }
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(0);
        }
        return sb.toString();
    }

    public String insertWithdrawal(MemberBank memberBank, String currentId, Integer type, Float payment,String businessId,Double serviceFee,Double withdrawalCharge) {
        Withdrawals withdrawal = new Withdrawals();
        withdrawal.setPayment(payment);
        withdrawal.setType(type);
        // 处理中
        withdrawal.setStatus(WithdrawalStatus.STATUS_IN_REVIEW.getCode());
        withdrawal.setCreateBy(currentId);
        withdrawal.setUpdateBy(currentId);
        withdrawal.setName(memberBank.getName());
        withdrawal.setAccountNumber(memberBank.getAccountNumber());
        withdrawal.setBankName(memberBank.getBankName());
        withdrawal.setOpenedPlace(memberBank.getOpenedPlace());
        withdrawal.setPhone(memberBank.getPhone());
        withdrawal.setOrderNum(VerifyCodeUtils.generateOrderNum(null));

        if(StringUtils.isNotBlank(businessId)){
            withdrawal.setProgress(WithdrawalAuditOperate.PROGRESS_APPLY);
        }
        withdrawal.setBusinessId(businessId);
        withdrawal.setServiceFee(serviceFee);
        withdrawal.setWithdrawalCharge(withdrawalCharge);

        return  withdrawalService.insert(withdrawal);
    }

    /**
     * 插入会员提现资金
     * @param mwf 会员提现资金
     * @return 事件编号
     */
    public void sevaOrupdateMemberWithdrawalFund(MemberWithdrawalFund mwf){
        MemberWithdrawalFund mwfObj=  memberWithdrawalFundService.findByMemberWithdrawalFund(mwf);
        if(null!= mwfObj){
            mwfObj.setSuccessNum(mwf.getSuccessNum());
            mwfObj.setMultiSuccessNum(mwf.getMultiSuccessNum());
            mwfObj.setTransferSuccessNum(mwf.getTransferSuccessNum());
            mwfObj.setTotalAleAmount(mwf.getTotalAleAmount());
            mwfObj.setType(mwf.getType());
            memberWithdrawalFundService.update(mwfObj);
        }else{
            memberWithdrawalFundService.insert(mwf);
        }
    }
}
